package com.ruoyi.web.controller.equipment;

import java.util.List;

import com.ruoyi.common.config.HeadNum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.equipment.domain.FzEquipment;
import com.ruoyi.equipment.service.IFzEquipmentService;
import com.ruoyi.framework.util.GetSessionValueUtil;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备管理Controller
 * 
 * @author ruoyi
 * @date 2020-03-22
 */
@Controller
@RequestMapping("/equipment/equipment")
public class FzEquipmentController extends BaseController
{
    private String prefix = "equipment/equipment";

    @Autowired
    private IFzEquipmentService fzEquipmentService;

    @RequiresPermissions("equipment:equipment:view")
    @GetMapping()
    public String equipment()
    {
        return prefix + "/equipment";
    }

    /**
     * 查询设备管理列表
     */
    @RequiresPermissions("equipment:equipment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FzEquipment fzEquipment)
    {
        startPage();
        fzEquipment.setcId(GetSessionValueUtil.getCommunity());
        List<FzEquipment> list = fzEquipmentService.selectFzEquipmentList(fzEquipment);
        System.out.println("");
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */

    @RequiresPermissions("equipment:equipment:export")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FzEquipment fzEquipment)
    {
        List<FzEquipment> list = fzEquipmentService.selectFzEquipmentList(fzEquipment);
        ExcelUtil<FzEquipment> util = new ExcelUtil<FzEquipment>(FzEquipment.class);
        return util.exportExcel(list, "equipment");
    }

    /**
     * 新增设备管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备管理
     */
    @RequiresPermissions("equipment:equipment:add")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody

    public AjaxResult addSave(FzEquipment fzEquipment)
    {
        fzEquipment.setEquipmentId(HeadNum.EQUIPMENT_HEAD+ DateUtils.getTimeMillis());
        fzEquipment.setcId(GetSessionValueUtil.getCommunity());
        fzEquipment.setCreateBy(ShiroUtils.getSysUser().getUserName());

        return toAjax(fzEquipmentService.insertFzEquipment(fzEquipment));
    }


    /**
     * 修改设备管理
     */
    @GetMapping("/edit/{equipmentId}")
    public String edit(@PathVariable("equipmentId") String equipmentId, ModelMap mmap)
    {
        FzEquipment fzEquipment = fzEquipmentService.selectFzEquipmentById(equipmentId);
        mmap.put("fzEquipment", fzEquipment);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备管理
     */
    @RequiresPermissions("equipment:equipment:edit")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FzEquipment fzEquipment)
    {
        fzEquipment.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return toAjax(fzEquipmentService.updateFzEquipment(fzEquipment));
    }

    /**
     * 删除设备管理
     */
    @RequiresPermissions("equipment:equipment:remove")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fzEquipmentService.deleteFzEquipmentByIds(ids));
    }
}
